package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.dllo.findfood.MainActivity;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.SplashActivity;
import com.lanou.dllo.findfood.util.CircleImage;

import java.io.FileNotFoundException;

/**
 * Created by dllo on 15/10/30.
 */
public class MyCenterActivity extends Activity implements View.OnClickListener {
    private ImageView goCenter;
    private LinearLayout chageName, changeImage;
    private TextView personname;
    private CircleImage circlePesonImage;
    private Button btn_exit;
    private ContentResolver cr;
    static final CharSequence[] items = {"本地相册", "相机拍摄", "取消"};
    static final CharSequence[] item = {"修改用户名", "取消"};
    private String name, personName,qqName;
    private  Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycenter);
        initView();
        getCentFragName();
      //  getQQName();
    }

    private void getCentFragName() {
        Intent intent = getIntent();
        name = intent.getStringExtra("Name");// 得到键值put对应的get,这个值来自于fragment
        personname.setText(name);
//        qqName = intent.getStringExtra("qqName");
//        personname.setText(qqName);
    }
private  void  getQQName(){

    qqName = getIntent().getStringExtra("qqName");
    personname.setText(qqName);
}
    private void initView() {
        goCenter = (ImageView) findViewById(R.id.goCenter);
        goCenter.setOnClickListener(this);
        personname = (TextView) findViewById(R.id.personNameTv);
        btn_exit = (Button) findViewById(R.id.btn_exit);
        btn_exit.setOnClickListener(this);
        circlePesonImage = (CircleImage) findViewById(R.id.personImage);
        circlePesonImage.setOnClickListener(this);
        cr = getContentResolver();
        chageName = (LinearLayout) findViewById(R.id.chageName);
        chageName.setOnClickListener(this);
        changeImage = (LinearLayout) findViewById(R.id.changeImage);
        changeImage.setOnClickListener(this);

    }



    private void setFragmentName() {
        Intent intent = new Intent();
        intent.putExtra("getName", personName);
        intent.putExtra("personImage",bitmap);//将图片传到fragment中
        setResult(500, intent);
        finish();
        overridePendingTransition(R.anim.push_left_in,
                R.anim.push_left_out);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.goCenter:
                setFragmentName();//将带有从change页面的值传回调给fragment
                break;
            case R.id.btn_exit:
                // 创建退出对话框
                AlertDialog isExit = new AlertDialog.Builder(this).create();
                // 设置对话框标题
                isExit.setTitle("退出");
                // 设置对话框消息
                isExit.setMessage("确定想从这个账号注销吗?");
                // 添加选择按钮并注册监听
                isExit.setButton("退出", listener);
                isExit.setButton2("取消", listener);
                // 显示对话框
                isExit.show();
                break;
            case R.id.changeImage:
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        this);
                builder.setTitle("请选择一个");
                //items使用全局的finalCharSequenece数组声明
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        String select_item = items[which].toString();
                        if (select_item == "本地相册") {
                            getImage();
                        }
                        if(select_item=="相机拍摄"){
                            try {
                                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                startActivityForResult(intent, 0);

                            } catch (ActivityNotFoundException e) {
                                 // Do nothing for now
                            }

                        }

                    }
                });
                builder.show();
                break;
            case R.id.chageName:
                AlertDialog.Builder build = new AlertDialog.Builder(
                        this);
                build.setTitle("修改用户名");
                //items使用全局的finalCharSequenece数组声明
                build.setItems(item, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        String select_item = item[which].toString();
                        if (select_item == "修改用户名") {
                            setChangeName();//将此时的姓名传给改变昵称页面
                        }

                    }
                });
                build.show();


                break;
            default:
                break;

        }
    }

    //将用户名的值传给修改昵称页面
    private void setChangeName() {
        Intent intent = new Intent(MyCenterActivity.this, ChangNameActivity.class);
        intent.putExtra("name", name);
    //    intent.putExtra("qqName",qqName);
        startActivityForResult(intent, 100);//带结果的回调
    }

    private void getImage() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//多媒体--图片--地址 跳转到图库地址
        startActivityForResult(intent, 100);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100 && resultCode == 100) {
            personName = data.getStringExtra("personName");
            personname.setText(personName);//从修改昵称的页面得到的值
        }
        //传图片到centerfragment
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();

            ContentResolver cr = this.getContentResolver();
            try {
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inSampleSize = 4;
                bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri),null,opts);
//                ImageView imageView = (ImageView) findViewById(R.id.personImage);
//                /* 将Bitmap设定到ImageView */
//                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {

            }
        }

        //访问本地图片的方法
        if (data != null && requestCode == 100 && resultCode == RESULT_OK) {
            Uri uri = data.getData();//图片中数据库的地址--图库地址
            Cursor cursor = cr.query(uri, null, null, null, null);
            if (cursor != null) {
                //遍历cursor对象
                while (cursor.moveToNext()) {
                    //获取文件路径地址
                    String imagePath = cursor.getString
                            (cursor.getColumnIndex(
                                    MediaStore.Images.Media.DATA));//取data字段 也就是图片对应的地址
                    //通过图片工厂去解析并显示
                    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    circlePesonImage.setImageBitmap(bitmap);
                }

                cursor.close();
            }
        }
    }

    /**
     * 监听对话框里面的button点击事件
     */
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
                    startActivity(new Intent(MyCenterActivity.this, SplashActivity.class));
                    finish();
                    overridePendingTransition(R.anim.push_below_in,
                            R.anim.push_below_out);
                    break;
                case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
                    break;
                default:
                    break;
            }
        }
    };
}
